package com.lihaozhe.handler;

import com.lihaozhe.util.response.ResponseResult;
import com.lihaozhe.util.result.ResultCode;
import com.lihaozhe.util.servlet.BaseServlet;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李昊哲
 * @version 1.0
 * 2021/12/1 下午2:34
 */
@Configuration
public class DefaultAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        BaseServlet.printJsonObject(response, new ResponseResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMsg()));
    }
}